package com.apartmentManage.controller;


import com.apartmentManage.dao.ApartmentDao;
import com.apartmentManage.entity.IntoPersonnel;
import com.apartmentManage.service.ApartmentService;
import com.apartmentManage.service.IntoPersonnelService;
import com.apartmentManage.service.RoomService;
import com.apartmentManage.util.Renter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/intoPersonnel")
public class IntoPersonnelController {

    @Autowired
    private IntoPersonnelService intoPersonnelService;

    @Autowired
    private ApartmentService apartmentService;

    @Autowired
   private RoomService roomService;

    @GetMapping("/addView")
    String addView(Model model){
        model.addAttribute("allNo",apartmentService.getAllNo());
        return "pages/intoPersonnel/add";
    }

    /*添加*/
    @PostMapping("/save")
    String save(IntoPersonnel intoPersonnel,Model model){
        if (intoPersonnel.getApartmentno()==0)
            return "redirect:/intoPersonnel/addView";
        boolean flag = intoPersonnelService.save(intoPersonnel);
        if (flag){
            List<Integer> allRoomNo = roomService.getByApartmentNo(intoPersonnel.getApartmentno());
            model.addAttribute("allRoomNo",allRoomNo);
            model.addAttribute("sno",intoPersonnel.getSno());
            return "pages/intoPersonnel/addRoom";
        }
        return "redirect:/intoPersonnel/addView";
    }

    /*添加房间*/
    @PostMapping("/saveRoom")
    String saveRoom(Integer roomno,String sno){
        boolean flag = intoPersonnelService.saveRoom(roomno, sno);
        if (flag)
            return "redirect:/intoPersonnel/getAll";
        intoPersonnelService.delete(sno);
        return "redirect:/intoPersonnel/addView";

    }

    @GetMapping("/updateView/{sno}")
    String updateView(@PathVariable String sno,Model model){
        IntoPersonnel result = intoPersonnelService.getByNo(sno);
        model.addAttribute("result",result);
        /**
         * 获取所有的房间号
         */
        List<Integer> allRoomNo = roomService.getByApartmentNo(result.getApartmentno());
        model.addAttribute("allRoomNo",allRoomNo);
        return "pages/intoPersonnel/update";
    }

    /*修改*/
    @PostMapping("/update")
    String update(IntoPersonnel intoPersonnel){
        boolean flag = intoPersonnelService.update(intoPersonnel);
        new Renter(flag);
        if (flag)
           return "redirect:/intoPersonnel/getAll";
        return "redirect:/intoPersonnel/updateView/"+intoPersonnel.getSno();
    }

    /*删除*/
    @GetMapping("/delete/{sno}")
    String delete(@PathVariable("sno") String sno){
        boolean flag = intoPersonnelService.delete(sno);
        new Renter(flag);
        return "redirect:/intoPersonnel/getAll";
    }

    /*查询所有*/
    @GetMapping("/getAll")
    String getAll(Model model){
        List<IntoPersonnel> result = intoPersonnelService.getAll();
        new Renter(result==null ? false : true,result);
        model.addAttribute("result",result);
        return "pages/intoPersonnel/List";
    }

//    @GetMapping("/getAll/{apartmentno}")
//    String getByApartment(Model model,@PathVariable Integer apartmentno){
//        List<IntoPersonnel> List = intoPersonnelService.getAll();
//        List<IntoPersonnel> result=new ArrayList<>();
//        for (IntoPersonnel intoPersonnel : List){
//            if (intoPersonnel.getApartmentno()==apartmentno)
//                result.add(intoPersonnel);
//        }
//        new Renter(result==null ? false : true,result);
//        model.addAttribute("result",result);
//        return "pages/intoPersonnel/List";
//    }

    /*条件模糊查询*/
    @GetMapping("/getByRequirement")
    String getByRequirement(IntoPersonnel intoPersonnel,Model model){
        List<IntoPersonnel> result = intoPersonnelService.getByRequirement(intoPersonnel);
        new Renter(result==null ? false : true,result);
       model.addAttribute("result",result);
        return "pages/intoPersonnel/List";

    }
}
